package com.linkwedo.saas.project;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkwedo.saas.domain.Time;

/**
 * A data access object (DAO) providing persistence and search support for
 * TbTask entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.db.TbTask
 * @author MyEclipse Persistence Tools
 */

public class TimeDao extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(TimeDao.class);
	// property constants
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String NOTE = "note";
	public static final String TASK_ID = "taskId";

	public void save(Time transientInstance) {
		log.debug("saving Time instance");
		Session curSession = getSession();
		try {
			curSession.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void delete(Time persistentInstance) {
		log.debug("deleting Time instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Time findById(java.lang.Integer id) {
		log.debug("getting Time instance with id: " + id);
		Session curSession = getSession();
		try {
			Time instance = (Time) curSession.get("com.huayanginfo.saas.domain.Time", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByExample(Time instance) {
		log.debug("finding Time instance by example");
		Session curSession = getSession();
		try {
			List results =curSession.createCriteria("com.huayanginfo.saas.domain.Time").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Time instance with property: " + propertyName
				+ ", value: " + value);
		Session curSession = getSession();
		try {
			String queryString = "from Time as model where model."
					+ propertyName + "= ?";
			Query queryObject = curSession.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByNote(Object note) {
		return findByProperty(NOTE, note);
	}

	public List findByTaskId(Object taskId) {
		return findByProperty(TASK_ID, taskId);
	}

	public List findAll() {
		log.debug("finding all Time instances");
		Session curSession = getSession();
		try {
			String queryString = "from com.huayanginfo.saas.domain.Time";
			Query queryObject = curSession.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Time merge(Time detachedInstance) {
		log.debug("merging Time instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			Time result = (Time) curSession.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}finally{
			curSession.close();
		}
		
	}

	public void attachDirty(Time instance) {
		log.debug("attaching dirty Time instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.saveOrUpdate(instance);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachClean(Time instance) {
		log.debug("attaching clean Time instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.lock(instance, LockMode.NONE);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}
}